package com.guchenbo.scala

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

import scala.beans.BeanProperty

/**
 * @author guchenbo
 * @date 2022/3/1
 */
object HelloJson {

  private lazy val _defaultMapper = new ObjectMapper().registerModule(DefaultScalaModule)

  def createMapper(): ObjectMapper = _defaultMapper

  protected final val mapper = createMapper()

  def main(args: Array[String]): Unit = {

    val str =
      """
        |{
        |  "database": "turing_monitor",
        |  "sql": "WyJTRUxFQ1QgKiBGUk9NIHR1cmluZ19tb25pdG9yLmdwX3Rlc3QwMSJd",
        |  "sparkJdbcOption": {
        |    "url": "jdbc:postgresql://10.57.36.55:5432/shangqi_db",
        |    "user": "gpadmin",
        |    "password": "gpadmin123",
        |    "driver": "org.postgresql.Driver",
        |    "tables": [
        |      {
        |        "jdbcTable": "public.test01",
        |        "sparkTable": "turing_monitor.gp_test01"
        |      }
        |    ]
        |  }
        |}
        |
        |""".stripMargin


    val paramConf = mapper.readValue(str, classOf[ParamConf])
    println(paramConf)
  }
}


case class ParamConf() {
  // 使用@BeanProperty注解，自动加入getter/setter方法
  @BeanProperty
  val database: String = "default"
  @JsonProperty("ETL_DATE")
  val etlDate: String = ""
  @JsonProperty("START_ETL_DATE")
  val startEtlDate: String = ""
  @JsonProperty("END_ETL_DATE")
  val endEtlDate: String = ""
  val sql: String = null
  val sqlSource: String = ""
  val sqlHdfsPath: String = ""
  val sparkJdbcOption: SparkJdbcOption = null
}

case class SparkJdbcOption() {
  val url: String = ""
  val user: String = ""
  val password: String = ""
  val driver: String = ""
  val tables: List[JdbcSparkTable] = List()
}

case class JdbcSparkTable() {
  val jdbcTable: String = ""
  val sparkTable: String = ""
}

